/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import BE.SeccionPt;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author CarLita
 */
public class SeccionPtDao {
    Session session;

    public boolean eliminarPorId(int idSeccion) {
        boolean ok =true;
        SeccionPt seccion = buscarPorId(idSeccion);
        
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = session.beginTransaction();
            session.delete(seccion);
            tx.commit();
            session.close();
        }
        catch(Exception e){
            ok = false;
            System.out.println(e.getMessage());
        }
        return ok;
    }

    public SeccionPt buscarPorId(int idSeccion) {
        session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from SeccionPt seccion_pt where seccion_pt.idSeccionPt= ? ";
        Query query = session.createQuery(hql);
        query.setInteger(0, idSeccion);
        List resultList = null;

        try {
            session.beginTransaction();
            resultList = query.list();
            session.getTransaction().commit();
            session.close();
            return (SeccionPt)resultList.get(0);
        } catch (HibernateException he) {
            he.printStackTrace();
        }

        session.close();
        return null;
    }

    public int guardar(SeccionPt s) {        
        session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        Object o = null;
        try {
            tx.begin();
            o = session.merge(s);
            tx.commit();
        } catch (HibernateException he){            
            tx.rollback();
            he.printStackTrace();
        } finally {
            session.close();
        }
        System.out.println("Seccion grabada:"+s.getIdSeccionPt());
        return o==null?-1:((SeccionPt)o).getIdSeccionPt();
        
    }

    public List buscarPorIdPapelTrabajo(int idPapel) {
               
        session = HibernateUtil.getSessionFactory().openSession();

        String hql = "from SeccionPt seccion_pt where seccion_pt.idPapelTrabajo= ? ";
        Query query = session.createQuery(hql);
        query.setInteger(0, idPapel);
        List resultList = null;

        try {
            session.beginTransaction();
            resultList = query.list();
            session.getTransaction().commit();
            session.close();
            return resultList;
        } catch (HibernateException he) {
            he.printStackTrace();
        }

        session.close();
        return null;
    }

    public List buscarNivelesPorPapelTrabajo(int idPapelTrabajo, int nivel, int idseccionPt) {
        session = HibernateUtil.getSessionFactory().openSession();

        String hql = "from SeccionPt seccion_pt where seccion_pt.idPapelTrabajo= ? ";        
        //nivel2
        if (nivel==2){
            hql+= "and seccion_pt.idseccionSuperior2= -1 " +
                    "and seccion_pt.idseccionSuperior1<> -1";                        
        }
        if (nivel ==3){
            hql+= "and seccion_pt.idseccionSuperior2<> -1 " +
                    "and seccion_pt.idseccionSuperior1<> -1";                        
        }
        
        Query query = session.createQuery(hql);
        query.setInteger(0, idPapelTrabajo);
        List resultList = null;

        try {
            session.beginTransaction();
            resultList = query.list();
            session.getTransaction().commit();
            session.close();
            return resultList;
        } catch (HibernateException he) {
            he.printStackTrace();
        }

        session.close();
        return null;
    }

    }
